package com.frogs.ucenter.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.frogs.framework.web.filter.SpringWebContext;
import com.frogs.ucenter.component.UserPermissionHelper;

public class LogonInterceptor extends HandlerInterceptorAdapter {
	private static final Logger log = LoggerFactory.getLogger(LogonInterceptor.class);

	/** 登录页链接地址 */
	private String loginURL;

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String url = request.getRequestURL().toString();
		log.info("func[preHandle] url[{}]", new Object[] {url});
		WebApplicationContext ctx = SpringWebContext.getSpringContext();
		UserPermissionHelper userPermissionHelper = (UserPermissionHelper) ctx.getBean("userPermissionHelper");
		if (userPermissionHelper.isLogin(request)) {
			return true;
		} else {
			request.setAttribute("ReturnUrl", request.getRequestURL().toString());
			response.sendRedirect(this.loginURL);
			return false;
		}
	}

	/**
	 * @return the loginURL
	 */
	public String getLoginURL() {
		return loginURL;
	}

	/**
	 * @param loginURL the loginURL to set
	 */
	public void setLoginURL(String loginURL) {
		this.loginURL = loginURL;
	}

}